<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 *
 * @package		Adminpro
 * @author		Tran Hoang Thien (thienhb12@gmail.com)
 * @copyright   PHP TEAM
 * @link		http://phpandmysql.net
 * @since		Version 1.0
 * @phone       0944418192
 *
 */
class Media extends MY_Controller {

	public function __construct(){
		parent:: __construct();
		$this->load->model('media_model');
		$this->load->model('media_error_model');
		$this->load->model('page_model');
		$this->load->model('category_model');
		$this->load->model('config_model');
	}

	public function index(){
		$data                      = array();
		$data['config']            = $this->cache_site->get_info_cache($key = 'config',$time = 86400,$this->config_model->get_config_info());
		$data['title']             = 'video';
		$data['keywords']          = $data['config']['cf_seo'];
		$data['description']       = $data['config']['cf_meta'];
		$data['phone']             = $data['config']['cf_phone'];
		$data['email']             = $data['config']['cf_email'];
		$data['result_cate']       = $this->cache_site->get_info_cache($key = 'cate',$time = 86400,$this->category_model->get_data_cate());
		$data['page']              = $this->cache_site->get_info_cache($key = 'page',$time = 86400,$this->page_model->get_list_page());
		$param                     = array();
		$param['select']           = array('idmedia','name','rewrite','thumb');
		$param['where']            = array('status' => 1);
		$url 					   = $this->uri->segment(1);
		$config['base_url']        = base_url().$url;
		$config['total_rows']      = $this->media_model->get_total($param);
		$config['per_page']        = $data['config']['cf_numrecord'];
		$config['uri_segment']     = '2';
		$config['first_link']      = '<<';
		$config['last_link']       = '>>';
		$config['first_tag_open']  = '<li class="click">';
		$config['first_tag_close'] = '</li>';
		$config['last_tag_open']   = '<li class="click">';
		$config['last_tag_close']  = '</li>';
		$config['prev_tag_open']   = '<li class="click">';
		$config['prev_tag_close']  = '</li>';
		$config['next_tag_open']   = '<li class="click">';
		$config['next_tag_close']  = '</li>';
		$config['num_tag_open']    = '<li class="click">';
		$config['num_tag_close']   = '</li>';
		$config['next_link']       = 'Next';
		$config['prev_link']       = 'Prev';
		$config['cur_tag_open']    = '<li class="active"><a>';
		$config['cur_tag_close']   = '</a></li>';
		$this->load->library("pagination");
		$this->pagination->initialize($config);
		$start                     = $this->uri->segment(2)?$this->uri->segment(2):0;
		$param['limit']			   = array($config['per_page'] ,$start);
		$data['result']            = $this->media_model->get_list($param);
		$data['result_slider']	   = $this->cache_site->get_info_cache($key='silder_video',$time = 3600,$this->media_model->slider_video());
		$data['template']          = 'media/media';
		$data['slider']            = 'slider/slider_video';
		$this->load->view("layout",$data);
	}

	public function detail(){
		$str                      = $this->uri->segment(3);
		$id                       = explode('-',$str);
		$id                       = (int)end($id);
		$data['result_cate']      = $this->cache_site->get_info_cache($key = 'cate');
		$param                    = array('name','url','rewrite','viewed','meta','nameseo');
		$data['page']             = $this->cache_site->get_info_cache($key = 'page');
		$data['result']           = $this->media_model->get_info($id,$param);
		$total_views              = $data['result']['viewed'] + 1;
		$this->media_model->save($id,array('viewed' => $total_views));
		$data['config']           = $this->cache_site->get_info_cache($key = 'config');
		$data['title']            = $data['result']['name'];
		$data['keywords']         = $data['result']['nameseo'];
		$data['description']      = $data['result']['meta'];
		$data['phone']            = $data['config']['cf_phone'];
		$data['email']            = $data['config']['cf_email'];
		$param_topvideo           = array();
		$param_topvideo['select'] = array('name','url','rewrite','viewed','idmedia');
		$param_topvideo['where']  = array('status' => 1);
		$param_topvideo['order']  = array('idmedia','DESC');
		$param_topvideo['limit']  = array(22);
		$data['ongoing']		  = $this->cache_site->get_info_cache($key='ongoing_video',$time= 1800,$this->media_model->get_list($param_topvideo));
		$data['template']         = 'media/watch';
		$data['curent']			  = current_url();
		$data['sidebar']          = 'siderbar_video';
		$data['urlMedia']         = str_replace('/watch?v=','/embed/',$data['result']['url']);
		$controls 				  = $this->input->post('controls');
		if(isset($controls)){
			$var                = array();
			if($controls == 'error-video'){
				$param['where'] = array('idmedia'=> $id  );
				$num = $this->media_error_model->get_total($param);
				if($num == 0){
					$db = array(
						'idmedia' => $id,
					);
					$this->media_error_model->save($id=false,$db);
					$var['error']   = 0;
					$var['content'] = 'error message successfully video';
				}else{
					$var['error']   = 1;
					$var['content'] = 'video was the error';
				}
				unset($controls);
				exit(json_encode($var));
			}
		}
		$this->load->view('layout',$data);
	}
}

/* End of file index.php */
/* Location: ./application/modules/home/controllers/index.php */